import request from '@/utils/request';
import { requestWrapper, randomInt, randomDate } from '@/utils/mock';

// 获取消息列表
export function getMessageList(params) {
  return requestWrapper(
    // 真实API请求
    () => request({
      url: '/message/list',
      method: 'get',
      params
    }),
    // 模拟数据
    () => {
      const pageNum = params?.pageNum || 1;
      const pageSize = params?.pageSize || 10;
      const total = 35; // 模拟总数
      
      // 消息类型过滤
      const messageType = params?.type;
      // 消息状态过滤
      const status = params?.status;
      
      // 生成消息数据
      const generateMessage = (index) => {
        const types = ['system', 'task', 'notice'];
        const priorities = ['low', 'medium', 'high'];
        const statuses = ['0', '1']; // 0-未读，1-已读
        const titles = [
          '系统通知',
          '任务提醒',
          '操作成功',
          '账户安全',
          '待办事项',
          '审批通知',
          '系统升级提醒',
          '密码即将过期'
        ];
        const contents = [
          '您的密码即将过期，请尽快修改',
          '您有一个待处理的任务',
          '您提交的申请已审批通过',
          '系统将于今晚22:00-23:00进行升级维护',
          '您有一个新的待办事项需要处理',
          '您的账户已完成安全设置',
          '您的申请已提交成功，等待审批',
          '系统检测到您的账户有异常登录尝试'
        ];
        const senders = ['system', 'admin', 'workflow', 'security'];
        
        // 根据传入的类型过滤
        let type = types[randomInt(0, types.length - 1)];
        if (messageType && types.includes(messageType)) {
          type = messageType;
        }
        
        // 根据传入的状态过滤
        let messageStatus = statuses[randomInt(0, statuses.length - 1)];
        if (status && statuses.includes(status)) {
          messageStatus = status;
        }
        
        // 根据类型选择标题和内容
        let title, content, sender, priority;
        switch (type) {
          case 'system':
            title = titles[randomInt(0, 1)];
            content = contents[randomInt(0, 1)];
            sender = 'system';
            priority = priorities[randomInt(1, 2)]; // 系统消息优先级较高
            break;
          case 'task':
            title = titles[randomInt(2, 4)];
            content = contents[randomInt(2, 4)];
            sender = senders[randomInt(1, 3)];
            priority = priorities[randomInt(0, 2)];
            break;
          case 'notice':
            title = titles[randomInt(5, 7)];
            content = contents[randomInt(5, 7)];
            sender = senders[randomInt(0, 1)];
            priority = priorities[randomInt(0, 1)]; // 公告优先级一般较低
            break;
          default:
            title = titles[randomInt(0, titles.length - 1)];
            content = contents[randomInt(0, contents.length - 1)];
            sender = senders[randomInt(0, senders.length - 1)];
            priority = priorities[randomInt(0, priorities.length - 1)];
        }
        
        return {
          messageId: index + 1,
          title,
          content,
          messageType: type,
          priority,
          status: messageStatus,
          sender,
          createTime: randomDate(-30, 0)
        };
      };
      
      // 模拟分页
      const startIndex = (pageNum - 1) * pageSize;
      const endIndex = Math.min(startIndex + pageSize, total);
      const list = Array.from({ length: endIndex - startIndex }, (_, i) => generateMessage(startIndex + i));
      
      return {
        list,
        total,
        pageNum: parseInt(pageNum),
        pageSize: parseInt(pageSize)
      };
    }
  );
}

// 获取消息详情
export function getMessageDetail(messageId) {
  return requestWrapper(
    // 真实API请求
    () => request({
      url: `/message/${messageId}`,
      method: 'get'
    }),
    // 模拟数据
    () => {
      const types = ['system', 'task', 'notice'];
      const priorities = ['low', 'medium', 'high'];
      const statuses = ['0', '1']; // 0-未读，1-已读
      const titles = [
        '系统通知',
        '任务提醒',
        '操作成功',
        '账户安全',
        '待办事项',
        '审批通知',
        '系统升级提醒',
        '密码即将过期'
      ];
      const contents = [
        '您的密码即将过期，请尽快修改密码以保障账户安全。登录系统后，进入个人中心-安全设置页面可修改密码。',
        '您有一个待处理的任务，请及时查看并处理。任务名称：月度报表审核，截止时间：2023-06-15',
        '您提交的申请已审批通过，申请编号：A2023060512，审批人：李经理，审批意见：同意',
        '系统将于今晚22:00-23:00进行升级维护，届时系统可能短暂不可用，请提前做好工作安排，给您带来的不便敬请谅解。',
        '您有一个新的待办事项需要处理，事项名称：客户投诉处理，优先级：高，来源：客服部',
        '您的账户已完成安全设置，包括密码强度验证、安全问题设置、绑定手机号和邮箱等，感谢您对账户安全的重视。',
        '您的申请已提交成功，申请编号：A2023060513，当前状态：等待审批，审批人：王总监',
        '系统检测到您的账户有异常登录尝试，如非本人操作，请立即修改密码并联系系统管理员。'
      ];
      const senders = ['system', 'admin', 'workflow', 'security'];
      const links = [
        '/user/security',
        '/task/detail/123',
        '/workflow/apply/A2023060512',
        '',
        '/task/todo/456',
        '/user/security',
        '/workflow/apply/A2023060513',
        '/user/security'
      ];
      
      // 根据ID选择消息内容
      const typeIndex = messageId % types.length;
      const titleIndex = messageId % titles.length;
      const contentIndex = messageId % contents.length;
      const senderIndex = messageId % senders.length;
      const linkIndex = messageId % links.length;
      
      return {
        messageId: parseInt(messageId),
        title: titles[titleIndex],
        content: contents[contentIndex],
        messageType: types[typeIndex],
        priority: priorities[messageId % priorities.length],
        status: statuses[messageId % statuses.length],
        sender: senders[senderIndex],
        createTime: randomDate(-30, 0),
        relatedLink: links[linkIndex]
      };
    }
  );
}

// 标记消息为已读
export function markMessageRead(messageIds) {
  return requestWrapper(
    // 真实API请求
    () => request({
      url: `/message/read/${messageIds}`,
      method: 'patch'
    }),
    // 模拟数据
    {
      success: true
    }
  );
}

// 删除消息
export function deleteMessage(messageIds) {
  return requestWrapper(
    // 真实API请求
    () => request({
      url: `/message/${messageIds}`,
      method: 'delete'
    }),
    // 模拟数据
    null
  );
}

// 获取未读消息数量
export function getUnreadMessageCount() {
  return requestWrapper(
    // 真实API请求
    () => request({
      url: '/message/unread/count',
      method: 'get'
    }),
    // 模拟数据
    {
      total: randomInt(1, 10),
      system: randomInt(0, 3),
      task: randomInt(0, 4),
      notice: randomInt(0, 3)
    }
  );
}

// 全部标记为已读
export function markAllRead(params) {
  return requestWrapper(
    // 真实API请求
    () => request({
      url: '/message/read/all',
      method: 'patch',
      params
    }),
    // 模拟数据
    {
      success: true
    }
  );
}

// 发送消息
export function sendMessage(data) {
  return requestWrapper(
    // 真实API请求
    () => request({
      url: '/message/send',
      method: 'post',
      data
    }),
    // 模拟数据
    {
      messageId: Date.now(),
      successCount: data.receivers?.length || 1,
      failCount: 0
    }
  );
}

// 获取消息通知设置
export function getMessageSetting() {
  return requestWrapper(
    // 真实API请求
    () => request({
      url: '/message/setting',
      method: 'get'
    }),
    // 模拟数据
    {
      emailNotify: true,
      smsNotify: false,
      appNotify: true,
      types: [
        {
          type: 'system',
          name: '系统消息',
          emailNotify: true,
          smsNotify: false,
          appNotify: true
        },
        {
          type: 'task',
          name: '任务消息',
          emailNotify: true,
          smsNotify: false,
          appNotify: true
        },
        {
          type: 'notice',
          name: '公告通知',
          emailNotify: false,
          smsNotify: false,
          appNotify: true
        }
      ]
    }
  );
}

// 更新消息通知设置
export function saveMessageSetting(data) {
  return requestWrapper(
    // 真实API请求
    () => request({
      url: '/message/setting',
      method: 'put',
      data
    }),
    // 模拟数据
    null
  );
} 